package com.ocom.payhandle.controller;

import com.ocom.common.constant.PayHandleUrl;
import com.ocom.common.entity.card.EntityCardInfo;
import com.ocom.common.entity.company.EntityCompanyConfig;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.MachEnum;
import com.ocom.common.request.payhandle.OnlineRepairQueryRequest;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.RespUtilMach;
import com.ocom.common.vo.pyhandle.WaitPayListVo;
import com.ocom.payhandle.service.CompanyConfigService;
import com.ocom.payhandle.service.PersonnelInfoService;
import com.ocom.payhandle.service.XfCardInfoService;
import com.ocom.payhandle.service.XfRecordWaitpayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/12/16
 **/
@Slf4j
@RestController
public class PersonController {

	@Autowired
	PersonnelInfoService personnelInfoService;

	@Autowired
	private XfCardInfoService xfCardInfoService;

	@Autowired
	private CompanyConfigService companyConfigService;

	@Autowired
	private XfRecordWaitpayService xfRecordWaitpayService;

	@PostMapping(value = PayHandleUrl.URL_getPersonByPhone)
	public RespMachMessage getPersonByPhone(Long comId, String phone){
		try {

			EntityPersonnelInfo entityPersonnelInfo = personnelInfoService.getPersonByPhone(comId,phone);
			if(entityPersonnelInfo==null){
				return RespUtilMach.error(MachEnum.NOT_USER);
			}
			return RespUtilMach.success(entityPersonnelInfo);

		}catch (Exception e){
			log.info("payhandle 人员获取异常【{}】",e);
		}
		return  null;
	}


	/**
	 * 2.4.12在线补缴订单查询（onlineRepairQuery）
	 */
	@PostMapping(value = PayHandleUrl.URL_onlineRepairQuery)
	public RespMachMessage onlineRepairQuery(@RequestBody OnlineRepairQueryRequest request){
		try {

			EntityCompanyConfig entityCompanyConfig = companyConfigService.getTapValue(request.getCompanyId(), "machineRepairPay");
			if (entityCompanyConfig != null && entityCompanyConfig.getConfig().equals("false") ) {
				return RespUtilMach.error(MachEnum.FAIL);
			}

			Long pId = null;
			if (request.getType() == 1) {
				//刷卡
				EntityCardInfo entityCardInfo = xfCardInfoService.selectCard(request.getCompanyId(), request.getAuthCode());
				if (entityCardInfo == null) {
					return RespUtilMach.error(MachEnum.NOT_USER);
				}
				pId = entityCardInfo.getPId();
				if (entityCardInfo.getCardState() != 1) {
					return RespUtilMach.error(MachEnum.CARDSTATERR);
				}
				if(entityCardInfo.getEndDate()!=null){
					String strNewDate = DateUtils.getStrDate(new Date(), "yyyy-MM-dd");
					String strEndDate = DateUtils.getStrDate(entityCardInfo.getEndDate(), "yyyy-MM-dd");
					if (!DateUtils.compare(strNewDate, strEndDate)) {
						return RespUtilMach.error(MachEnum.CARDSTATERR);
					}
				}
			} else {
				pId = Long.valueOf(request.getAuthCode());
			}
			List<WaitPayListVo> pageInfo =  xfRecordWaitpayService.getXfRecordWaitpay(request.getCompanyId(),pId);
			if(pageInfo.size()>0) 	return RespUtilMach.success(pageInfo);
			return RespUtilMach.error(MachEnum.PAY_NOT_EXIST);
		}catch (Exception e) {
				StackTraceElement ste = e.getStackTrace()[0];
				log.info("请求方式67:【{}】,机器号:【{}】,请求参数:【{}】,异常2:【{}】,行号:【{}】", "支付请求（强在线）", request.getMachNo(), request, e, ste.getLineNumber());
			return RespUtilMach.error(MachEnum.ERR);
		}
	}

}
